package com.edu.controller.order;

import com.edu.entity.Food;
import com.edu.entity.Order;
import com.edu.entity.User;
import com.edu.service.food.FoodService;
import com.edu.service.food.FoodServiceImpl;
import com.edu.service.ordertab.OrderService;
import com.edu.service.ordertab.OrderServiceImpl;
import com.edu.service.user.UserService;
import com.edu.service.user.UserServiceImpl;
import com.edu.utils.ParamFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ZCN
 * @version 1.0
 * @date 2021/11/25 17:24
 */
@WebServlet("/order/queryorder")
public class QueryOrderServlet extends HttpServlet {
    private OrderService orderService = new OrderServiceImpl();
    private UserService userService = new UserServiceImpl();
    private FoodService foodService = new FoodServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            ParamFormat.format(req, resp);
            List<Order> orders = orderService.selectAll();

            for (Order order : orders) {
                Integer userid = order.getUserid();
                User user = userService.selectByUsername(userid);
                // 设置购买者name
                order.setUsername(user.getUsername());

                //设置菜品名称
                String foodname = "";
                String payfoodid = order.getPayfoodid();
                String[] foodids = payfoodid.split(",");
                for (String foodid : foodids) {
                    Food food = foodService.findOne(Integer.valueOf(foodid));
                    foodname += food.getFoodname()+",";
                }
                foodname = foodname.substring(0,foodname.length()-1);

                order.setFoodname(foodname);
            }


            req.setAttribute("orders",orders);
            req.getRequestDispatcher("/system/order.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
